package jus.aor.mobilagent.annuaire;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.HashMap;

public class Annuaire extends UnicastRemoteObject implements _Annuaire{

	private static final long serialVersionUID = 1L;
	private HashMap<String,ArrayList<String>> annuaire;
	private ArrayList<String> listServiceDefaut;
	
	public Annuaire() throws RemoteException{
		annuaire = new HashMap<String,ArrayList<String>>();
		listServiceDefaut = new ArrayList<String>();
	}

	@Override
	public ArrayList<String> getServeur(String service) throws RemoteException {
		ArrayList<String> prov = new ArrayList<String>();
		for(String serv : annuaire.keySet())
			if(annuaire.get(serv).contains(service))
				prov.add(serv);
		return prov;
	}

	@Override
	public ArrayList<String> getServeur(String service,int nb) throws RemoteException {
		ArrayList<String> prov = new ArrayList<String>();
		for(String serv : annuaire.keySet())
			if(annuaire.get(serv).contains(service))
				prov.add(serv);
		return new ArrayList<String>(prov.subList(0, nb));
	}

	@Override
	public ArrayList<String> getService(String server) throws RemoteException {	
		return annuaire.get(server);
	}

	@Override
	public void addServer(String server) throws RemoteException {
		annuaire.put(server, new ArrayList<String>(listServiceDefaut));
	}

	@Override
	public void addService(String server, String service)throws RemoteException {
		annuaire.get(server).add(service);
	}
	
	public void addServiceDefaut(String service){
		listServiceDefaut.add(service);
	}

}
